
import javax.mail.*;
import javax.mail.internet.*;

import java.security.Security;
import java.util.*;
import javax.activation.*;

public class Mail {

	public static String host="smtp.gmail.com";
	public static String from="lenserver.su@gmail.com";
	public static String username="lenserver.su@gmail.com";
	public static String password="pacpsp12";
	public static String content="This email is sended by system auto.Don't reply!";

	/**
	 * @param args
	 * @throws Exception
	 */
	
	public static void send(String to,String subject,String fileAttachment) throws Exception
	{
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		String SSL_FACTORY="javax.net.ssl.SSLSocketFactory";
		
    	Properties props=new Properties();
    	props.setProperty("mail.smtp.host", "smtp.gmail.com");
    	props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    	props.setProperty("mail.smtp.socketFactory.fallback", "false");
    	props.setProperty("mail.smtp.port", "465");
    	props.setProperty("mail.smtp.socketFactory.port", "465");

    	props.put("mail.smtp.host", host);
    	props.put("mail.smtp.auth", "true");
    	
    	Session s=Session.getDefaultInstance(props);
    	
    	MimeMessage msg=new MimeMessage(s);
    	msg.setFrom(new InternetAddress(from));
    	msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
    	msg.setSentDate(new Date());
    	msg.setSubject(subject+" from prediction result from Len-server");
    	//create the message part
    	MimeBodyPart messageBodyPart=new MimeBodyPart();
    	messageBodyPart.setText(content);
    	
    	Multipart multipart=new MimeMultipart();
    	multipart.addBodyPart(messageBodyPart);
    	
    	//file attach
    	for(int i=1;i<=5;i++)
    	{
    	
    	messageBodyPart=new MimeBodyPart();
    	DataSource source=new FileDataSource(fileAttachment+"_"+i+".pdb");
    	messageBodyPart.setDataHandler(new DataHandler(source));;
    	messageBodyPart.setFileName(subject+"_"+i+".pdb");
    	multipart.addBodyPart(messageBodyPart);
    	}
    	
    	msg.setContent(multipart);
    	
    	msg.saveChanges();
    	
    	Transport transport=s.getTransport("smtp");
    	transport.connect(host,username, password);
    	 transport.sendMessage(msg, msg.getAllRecipients());
    	transport.close();
    	System.out.println("Send Email Successfully");
  
	}

}
